﻿using CarAppClient.Consts;
using CarAppClient.Controls.MessageUsercontrols;
using CarAppClient.Helpers;
using CarAppClient.ViewModels;
using CarAppClient.Views.Sys.Views;
using CarAppDto.Common;
using CarAppDto.Sys.AdminDtos;
using D.UtilCore.Dtos;

namespace CarAppClient.Views.Sys.ViewModels
{
    public class UserModifyViewModel : BaseModifyViewModel<Page_User_Modify>
    {
        #region 加载页面数据

        public UserModifyViewModel(Page_User_Modify owner, string? id, ViewStatus? viewStatus) : base(owner, id, viewStatus)
        {
        }

        /// <summary>
        /// 查询
        /// </summary>
        protected override async void LoadData()
        {
            if (!string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<RepAdminInfo>.GetAsync($"{ApiUrl.GetAdmin}/{Id}");

                //  判断结果
                if (res.Code == StatusCodeEnum.Waring)
                {
                    MsgUtil.Error(res.ErrorMsg);
                }
                else if (res.Code == StatusCodeEnum.Error)
                {
                    MsgUtil.Error("服务器异常");
                }
                else
                {
                    // 绑定数据源到UI 
                    if (res.Data != null)
                    {
                        UUID = res.Data.UUID;
                        Account = res.Data.Account;
                        NickName = res.Data.NickName;
                        Email = res.Data.Email;
                        Status = res.Data.Status;
                        Descption = res.Data.Descption;
                    }
                }
            }
        }
        #endregion

        #region 新增、修改 参数

        private string? uuid;
        /// <summary>
        /// 工号
        /// </summary>
        public string? UUID
        {
            get { return uuid; }
            set { uuid = value; RaisePropertyChanged(); }
        }

        private string? account;
        /// <summary>
        /// 账号
        /// </summary>
        public string? Account
        {
            get { return account; }
            set { account = value; RaisePropertyChanged(); }
        }

        private string? pwd;

        /// <summary>
        /// 密码
        /// </summary>
        public string? Pwd
        {
            get { return pwd; }
            set { pwd = value; RaisePropertyChanged(); }
        }

        private string? passwordConfirm;

        /// <summary>
        /// 二次密码确认
        /// </summary>
        public string? PasswordConfirm
        {
            get { return passwordConfirm; }
            set { passwordConfirm = value; RaisePropertyChanged(); }
        }

        private string? nickName;

        /// <summary>
        /// 昵称
        /// </summary>
        public string? NickName
        {
            get { return nickName; }
            set { nickName = value; RaisePropertyChanged(); }
        }

        private string? email;

        /// <summary>
        /// 昵称
        /// </summary>
        public string? Email
        {
            get { return email; }
            set { email = value; RaisePropertyChanged(); }
        }

        private string? descption;

        /// <summary>
        /// 描述
        /// </summary>
        public string? Descption
        {
            get { return descption; }
            set { descption = value; RaisePropertyChanged(); }
        }

        private bool status;

        /// <summary>
        /// 状态
        /// </summary>
        public bool Status
        {
            get { return status; }
            set { status = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 保存事件
        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="vm"></param>
        protected async override void Submit(object? obj)
        {
            // 基础数据判断
            if (string.IsNullOrEmpty(NickName))
            {
                MsgUtil.Warning("昵称不可为空"); return;
            }

            if (string.IsNullOrEmpty(Account))
            {
                MsgUtil.Warning("账户名不可为空"); return;
            }

            if (string.IsNullOrEmpty(Id))
            {
                if (string.IsNullOrEmpty(Pwd))
                {
                    MsgUtil.Warning("密码不可为空"); return;
                }

                if (string.IsNullOrEmpty(PasswordConfirm))
                {
                    MsgUtil.Warning("二次密码不可为空"); return;
                }

                if (Pwd != PasswordConfirm)
                {
                    MsgUtil.Warning("俩次密码输入不一致"); return;
                }
            }
            // 基础数据判断
            if (string.IsNullOrEmpty(Id))
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PostAsync(ApiUrl.AddAdmin, new ReqAddAdmin()
                {
                    UUID = UUID,
                    NickName = NickName,
                    Account = Account,
                    Pwd = Pwd,
                    PasswordConfirm = PasswordConfirm,
                    Email = Email,
                    Status = Status,
                    Descption = Descption
                });

                //  判断结果
                MsgUtil.ShowMsg(res.Code, "新增成功", res.ErrorMsg);
            }
            else
            {
                // 请求后端接口 
                var res = await HttpHelper<int>.PutAsync($"{ApiUrl.UpdateAdmin}/{Id}", new ReqUpdateAdmin()
                {
                    Id = Id,
                    UUID = UUID,
                    NickName = NickName,
                    Email = Email,
                    Status = Status,
                    Descption = Descption
                });
                // 判断结果
                MsgUtil.ShowMsg(res.Code, "修改成功", res.ErrorMsg);
            }
        }
        #endregion 
    }
}
